package com.pucong.gitee.database.backup.business.service.util;

import com.pucong.gitee.database.backup.business.service.vo.ServiceFileVo;
import com.pucong.gitee.database.backup.common.entity.service.ServiceFile;
import com.pucong.gitee.database.backup.configuration.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

@Slf4j
public class FileOperationServiceFastdfsImpl extends AbstractFileOperationService {

    private TrackerServer trackerServer;

    private StorageClient storageClient;

    public FileOperationServiceFastdfsImpl(ServiceFile serviceFile) {
        super(serviceFile);
        synchronized (FileOperationServiceFastdfsImpl.class) {
            init();
            TrackerClient trackerClient = new TrackerClient();
            try {
                TrackerServer trackerServer = trackerClient.getTrackerServer();
                StorageClient storageClient = new StorageClient(trackerServer, null);
                this.trackerServer = trackerServer;
                this.storageClient = storageClient;
            } catch (IOException e) {
                log.error("初始化fastdfs失败", e);
                throw new BusinessException("初始化fastdfs失败");
            }
        }
    }

    private void init() {
        String trackerServers = serviceFile.getHost() + ":" + serviceFile.getPort();
        Properties props = new Properties();
        props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, trackerServers);
        props.put(ClientGlobal.g_tracker_group, serviceFile.getGroupName());
        try {
            ClientGlobal.initByProperties(props);
        } catch (Exception e) {
            log.error("初始化fastdfs失败", e);
            throw new BusinessException("初始化fastdfs失败");
        }
    }

    @Override
    public List<ServiceFileVo> getFileServerCatalogue(String path) {
        return null;
    }

    @Override
    public boolean uploadFile(String filePath, InputStream inputStream, String outputPath, String fileName) {
        try {
            String[] arr = null;
            synchronized (storageClient) {
                init();
                arr = storageClient.upload_file(filePath, null, null);
            }
            if (arr == null || arr.length != 2) {
                return false;
            } else {
                return true;
            }
        } catch (IOException | MyException e) {
            log.error("向FastDFS上传文件 error", e);
        }
        return false;
    }

    @Override
    public boolean addCatalogue(String filePath, String addName) {
        throw new BusinessException("无法实现该功能");
    }

    @Override
    public boolean deleteFile(String filePath) {
        try {
            int i = 0;
            synchronized (storageClient) {
                init();
                i = storageClient.delete_file(serviceFile.getGroupName(), filePath);
            }
            if (i == 0) {
                return false;
            } else {
                return true;
            }
        } catch (IOException | MyException e) {
            log.error("FastDFS删除文件error", e);
        }
        return false;
    }

    @Override
    public boolean deleteCatalogue(String filePath) {
        throw new BusinessException("无法实现该功能");
    }

    @Override
    public void downloadFile(OutputStream out, String filePath) {
        try {
            byte[] content = null;
            synchronized (storageClient) {
                init();
                content = storageClient.download_file(serviceFile.getGroupName(), filePath);
            }
            if (content == null || content.length == 0) {
                log.error("文件大小为空！");
            }
            out.write(content);
        } catch (IOException | MyException e) {
            log.error("FastDFS下载文件error", e);
        }
    }

    @Override
    public void lookFile(OutputStream out, String filePath) {
        downloadFile(out, filePath);
    }

    @Override
    public void close() {
        throw new BusinessException("无法实现该功能");
    }
}
